﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GISCore.Geometry;
using GISCore.Geometry.IO;
using GISCore.Geometry.Intersectors;
using GISCore.Geometry.Partition;
using GISCore.Geometry.Merge;

namespace GISCore
{
    class Program  
    { 
        static void Main(string[] args)
        {
            //testTopologyReconstruction();
            //testSweepline();
            //testPartition();
            //parserTest();
            //fullTest();
            //testCellNeighbours();
            //testMerging();
            //construction();
            //debugSweepLine();
            SweepLineIITester();
            Console.Read();

        }

        //Please dont comment the following method 
        // and check in. Thanks 
        #region fulltest
        
        public static void fullTest()
        {
            int total = 0;
            int rowCount = 1;
            int colCount = 1;
            IFileHandler handler_base = new GMLFileHandler(Constants.baseLayerId);
            IFileHandler handler_target = new GMLFileHandler(Constants.targetLayerId);

            string filename = "../../../GISUnitTests/Test Data/polygon1subset.gml";
            string filename2 = "../../../GISUnitTests/Test Data/polygon1subset_50.gml";
            //string filename = "../../../GISUnitTests/Test Data/layerAA.gml";
            //string filename2 = "../../../GISUnitTests/Test Data/layerBB.gml";

            IEnumerable<LinkedPoint> uptemp = null;
            IEnumerable<LinkedPoint> downtemp = null;
            IEnumerable<LinkedPoint> lefttemp = null;
            IEnumerable<LinkedPoint> righttemp = null;
           
            Layer layer_base = handler_base.Read(filename);
            Layer layer_target = handler_target.Read(filename2);

            IPartitionable partitioner = new UniformGrid(rowCount, colCount);
            IDictionary<int,ICellable> cells = partitioner.Partition(layer_base, layer_target);

            IIntersector intersector = new SweepLineII();
            //IIntersector intersector = new AllPairsIntersector();
            IReconstuctionable reconstructor = new PolygonReconstuctor();
            Cell temp=null;
            foreach (var cell in cells.Values)
            {
                IEnumerable<Line> lines = cell.getLines();
                IEnumerable<IntersectPoint> points = intersector.Intersect(lines);
                foreach (var l in lines)
                {
                    l.SortPoints();
                }
                (cell as Cell).setIntersectPoint(points);

                if((rowCount ==1)&&(colCount == 1)){
                    //(cell as Cell).setBottomGridIntersectPoint(
                }
                (cell as Cell).xmin = (cell as Cell).xmin - 1;
                (cell as Cell).xmax = (cell as Cell).xmax + 1;
                (cell as Cell).ymin = (cell as Cell).ymin - 1;
                (cell as Cell).ymax = (cell as Cell).ymax + 1;
                (cell as Cell).postPolys = reconstructor.reconstruct(cell.getLines(), cell.getIntersectPoint(), cell.getTopGridIntersectPoint(), cell.getBottomGridIntersectPoint(), cell.getLeftGridIntersectPoint(), cell.getRightGridIntersectPoint(), out uptemp, out downtemp, out lefttemp, out righttemp, (cell as Cell).xmin, (cell as Cell).xmax, (cell as Cell).ymin, (cell as Cell).ymax) as List<PostPolygon>;
                (cell as Cell)._upLinkedPoints = uptemp as List<LinkedPoint>;
                (cell as Cell)._downLinkedPoints = downtemp as List<LinkedPoint>;
                (cell as Cell)._leftLinkedPoints = lefttemp as List<LinkedPoint>;
                (cell as Cell)._rightLinkedPoints = righttemp as List<LinkedPoint>;
                temp=cell as Cell;

                //cell.setPostPolygons(reconstructor.reconstruct(cell.getLines(), cell.getIntersectPoint(), cell.getTopGridIntersectPoint(), cell.getBottomGridIntersectPoint(), cell.getLeftGridIntersectPoint(), cell.getRightGridIntersectPoint()));
            }

            foreach (var p in (layer_base.GetPolygons() as List<Polygon>)[0].getPoints())
            {
                Console.Write(p+"|||");
            }
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");
            
            foreach (var p in (layer_target.GetPolygons() as List<Polygon>)[0].getPoints())
            {
                Console.Write(p+"|||");
            }

            
             IMergable merger = new LinkedMerge();
             //merger.merge(cells as Dictionary<int,Cell>);
             IFileHandler handler_overlayer = new GMLFileHandler(Constants.overLayerId);
             //handler_overlayer.Write(merger.merge(cells as Dictionary<int,Cell>), "../../../GISUnitTests/Test Data/result.gml");

             handler_overlayer.Write(new LinkedMerge()._constructLayer(temp.getPostPolygons()), "../../../GISUnitTests/Test Data/result.gml");
             Console.Read();
            /*
            IMergable merger = new LinkedMerge();
            IFileHandler handler_overlayer = new GMLFileHandler(Constants.overLayerId);
            handler_overlayer.Write(merger.merge(cells), "");
            */

            // #region fulltest
            /*
            UniformGrid p = new UniformGrid(rowCount, colCount);

            var cells = p.Partition(layer_base, layer_target);

            Cell[,] cellsMatrix = new Cell[rowCount, colCount];

            int row=0,col=0;
            /*
            foreach (var cell in cells)
            {
                int cellID = cell.CellID;
                getRowAndColumnIDbyCell_Id(cellID,ref row,ref col,colCount);
                cellsMatrix[row - 1, col - 1] = cell;
            }
            */
            SweepLine sweeper = new SweepLine();
            foreach (var cell in cells.Values)
            {
                var points = sweeper.Intersect(cell.getLines()).ToList();
                total += points.Count;
                //cell.setPoints(points);
            }

            // #endregion
            
        }
        
        #endregion
        private static void getRowAndColumnIDbyCell_Id(int Cell_Id, ref int row, ref int column,
            int columnCount)
        {
            column = Cell_Id % columnCount;
            if (column == 0)
                column = columnCount;

            row = (int)Math.Ceiling((double)Cell_Id / columnCount);
        }

        public static void checkPartition()
        {
            double CellSize = 2;
            int layer = 1;
            string filename = "../../../GISUnitTests/Test Data/gmlTest.gml";
            IFileHandler handler = new GMLFileHandler(layer);
            Layer layer2 = handler.Read(filename);

            string filename2 = "../../../GISUnitTests/Test Data/layerA.gml";
            handler = new GMLFileHandler(2);
            Layer layer1 = handler.Read(filename2);

            
            UniformGrid plane = new UniformGrid(10, 10);
            var cells = plane.Partition(layer1, layer2);
        }

        public static void testCellNeighbours()
        {
            Point a = new Point(3.5,3.3);
            Point b = new Point(4.8,1.5);
            Point c = new Point(1.6,1.4);
            Point d = new Point(1.5,2.3);
        
            Line l1 = new Line(a,b);
            Line l2 = new Line(b,c);
            Line l3 = new Line(c,d);
            Line l4 = new Line(d,a);

            l1.next = l2;
            l2.prev = l1;
            l2.next = l3;
            l3.prev = l2;
            l3.next = l4;
            l4.prev = l3;
            l4.next = l1;

            List<Line> lines = new List<Line>();
            lines.Add(l1);
            lines.Add(l2);
            lines.Add(l3);
            lines.Add(l4);
            Polygon poly1 = new Polygon(1, lines);
            l1.polygon = poly1;
            l2.polygon = poly1;
            l3.polygon = poly1;
            l4.polygon = poly1;

            List<Polygon> polygons = new List<Polygon>();
            polygons.Add(poly1);

            Layer layer = new Layer(0, polygons);
            UniformGrid plane = new UniformGrid(10, 10);

            var cells = plane.Partition(layer, layer);
        }
                                                                                                                                                                                                                                                                                            #region satish
        /* 
        public static void testPartition(List<Polygon> polygons)
         {
             
             Layer layer1 = null;//new Layer();
             Layer layer2 = null;//new Layer();
             UniformGrid plane = new UniformGrid(10, 10);
             var cells = plane.Partition(layer1, layer2);
         }

         private static List<Polygon> getSamplePolygon()
         {
             List<Polygon> polygons = new List<Polygon>();

             Point pa = new Point(2, 4);
             Point pd = new Point(4, 6);
             Point pc = new Point(6, 4);
             Point pb = new Point(4, 2);
             Point p4 = new Point(4, 3);
             Point p3 = new Point(7, 6);
             Point p2 = new Point(9, 4);
             Point p1 = new Point(7, 2);



             Line lad = new Line(pa, pd);
             Line ldc = new Line(pd, pc);
             Line lcb = new Line(pc, pb);
             Line lba = new Line(pb, pa);
             Line l43 = new Line(p4, p3);
             Line l32 = new Line(p3, p2);
             Line l21 = new Line(p2, p1);
             Line l14 = new Line(p1, p4);

             lad.next = ldc;
             lad.prev = lba;
             ldc.next = lcb;
             ldc.prev = lad;
             lcb.next = lba;
             lcb.prev = ldc;
             lba.next = lad;
             lba.prev = lcb;

             l43.next = l32;
             l43.prev = l14;
             l32.next = l21;
             l32.prev = l43;
             l21.next = l14;
             l21.prev = l32;
             l14.next = l43;
             l14.prev = l21;


             List<Line> l1 = new List<Line>();
             l1.Add(lad);
             l1.Add(ldc);
             l1.Add(lcb);
             l1.Add(lba);
             Polygon poly1 = new Polygon(1, l1);
             lad.polygon = poly1;
             ldc.polygon = poly1;
             lcb.polygon = poly1;
             lba.polygon = poly1;

             List<Line> l2 = new List<Line>();
             l2.Add(l43);
             l2.Add(l32);
             l2.Add(l21);
             l2.Add(l14);
             Polygon poly2 = new Polygon(2, l2);
             l43.polygon = poly2;
             l32.polygon = poly2;
             l21.polygon = poly2;
             l14.polygon = poly2;

             polygons.Add(poly1);
             polygons.Add(poly2);
             return polygons;
         }
         */
#endregion
        public static void debugSweepLine()
        {
            Point x1 = new Point(8, 12);
            Point x2 = new Point(10, 8);
            Point x3 = new Point(9, 4.5);
            Point x4 = new Point(4, 4);
            Point x5 = new Point(0.5, 5);
            Point x6 = new Point(2, 8);

            Line lx12 = new Line(x1, x2);
            Line lx23 = new Line(x2, x3);
            Line lx34 = new Line(x3, x4);
            Line lx45 = new Line(x4, x5);
            Line lx56 = new Line(x5, x6);
            Line lx61 = new Line(x6, x1);

            List<Line> temp = new List<Line>();
            temp.Add(lx12);
            temp.Add(lx23);
            temp.Add(lx34);
            temp.Add(lx45);
            temp.Add(lx56);
            temp.Add(lx61);

            Polygon polyx1 = new Polygon(1, temp);
            lx12.setPolygon(polyx1);
            lx23.setPolygon(polyx1);
            lx34.setPolygon(polyx1);
            lx45.setPolygon(polyx1);
            lx56.setPolygon(polyx1);
            lx61.setPolygon(polyx1);

            Point x7 = new Point(8.1, 10);
            Point x8 = new Point(7, 7);
            Point x9 = new Point(5, 7.1);
            Point x10 = new Point(7.5, 10.5);

            Line lx710 = new Line(x7, x10);
            Line lx109 = new Line(x10, x9);
            Line lx98 = new Line(x9, x8);
            Line lx87 = new Line(x8, x7);

            List<Line> temp1 = new List<Line>();
            temp1.Add(lx710);
            temp1.Add(lx109);
            temp1.Add(lx98);
            temp1.Add(lx87);
            Hole Holex7 = new Hole(1, temp1);
            polyx1.addHole(Holex7);
            lx710.setPolygon(Holex7);
            lx109.setPolygon(Holex7);
            lx98.setPolygon(Holex7);
            lx87.setPolygon(Holex7);

            Point y1 = new Point(3, 8);
            Point y2 = new Point(6, 9);
            Point y3 = new Point(8.2, 8);
            Point y4 = new Point(8, 6);
            Point y5 = new Point(6.5, 2);
            Point y6 = new Point(1, 1);

            Line ly12 = new Line(y1, y2);
            Line ly23 = new Line(y2, y3);
            Line ly34 = new Line(y3, y4);
            Line ly45 = new Line(y4, y5);
            Line ly56 = new Line(y5, y6);
            Line ly61 = new Line(y6, y1);

            List<Line> temp5 = new List<Line>();
            temp5.Add(ly12);
            temp5.Add(ly23);
            temp5.Add(ly34);
            temp5.Add(ly45);
            temp5.Add(ly56);
            temp5.Add(ly61);
            Polygon polyy1 = new Polygon(2, temp5);
            ly12.setPolygon(polyy1);
            ly23.setPolygon(polyy1);
            ly34.setPolygon(polyy1);
            ly45.setPolygon(polyy1);
            ly56.setPolygon(polyy1);
            ly61.setPolygon(polyy1);

            Point y7 = new Point(7, 7.5);
            Point y8 = new Point(7.1, 6.5);
            Point y9 = new Point(6.5, 5.5);
            Point y10 = new Point(6.6, 7.3);

            Line ly710 = new Line(y7, y10);
            Line ly109 = new Line(y10, y9);
            Line ly98 = new Line(y9, y8);
            Line ly87 = new Line(y8, y7);
            List<Line> temp6 = new List<Line>();
            temp6.Add(ly710);
            temp6.Add(ly109);
            temp6.Add(ly98);
            temp6.Add(ly87);
            Hole holey7 = new Hole(2, temp6);
            polyy1.addHole(holey7);
            ly710.setPolygon(holey7);
            ly109.setPolygon(holey7);
            ly98.setPolygon(holey7);
            ly87.setPolygon(holey7);

            GridIntersectPoint k1 = new GridIntersectPoint(1, 6);
            k1.setLine(lx56);
            GridIntersectPoint k2 = new GridIntersectPoint(2.4, 6);
            k2.setLine(ly61);
            GridIntersectPoint k3 = new GridIntersectPoint(5, 10);
            k3.setLine(lx61);
            GridIntersectPoint k4 = new GridIntersectPoint(5, 8.7);
            k4.setLine(ly12);
            GridIntersectPoint k5 = new GridIntersectPoint(6.5, 6);
            k5.setLine(ly109);
            GridIntersectPoint k6 = new GridIntersectPoint(6.8, 6);
            k6.setLine(ly98);
            GridIntersectPoint k7 = new GridIntersectPoint(8, 6);
            k7.setLine(ly34);
            GridIntersectPoint k8 = new GridIntersectPoint(9.4, 6);
            k8.setLine(lx23);
            GridIntersectPoint k9 = new GridIntersectPoint(5, 4.1);
            k9.setLine(lx34);
            GridIntersectPoint k10 = new GridIntersectPoint(5, 1.7);
            k10.setLine(ly56);
            GridIntersectPoint k11 = new GridIntersectPoint(8, 12);
            GridIntersectPoint k12 = new GridIntersectPoint(10, 8);
            GridIntersectPoint k13 = new GridIntersectPoint(5, 7.1);
            

            Line lk3x1 = new Line(k3, x1);
            lk3x1.parent = lx61;
            Line lx2k8 = new Line(x2, k8);
            lx2k8.parent = lx23;
            Line lk4y2 = new Line(k4, y2);
            lk4y2.parent = ly12;
            Line ly3k7 = new Line(y3, k7);
            ly3k7.parent = ly34;

            Line ly10k5 = new Line(y10, k5);
            Line lk6y8 = new Line(k6, y8);


            //Cell cell11 = new Cell(4);

            //cell11.addLine(lx2k8);
            //cell11.addLine(lk4y2);
            //cell11.addLine(ly23);
            //cell11.addLine(ly3k7);
            //cell11.addLine(lk3x1);
            //cell11.addLine(lx12);
           
            
            //adding lines in the hole
            //cell11.addLine(lx109);
            //cell11.addLine(lx98);
            //cell11.addLine(lx87);
            //cell11.addLine(lx710);
            //cell11.addLine(ly10k5);
            //cell11.addLine(lk6y8);
            //cell11.addLine(ly87);
            //cell11.addLine(ly710);
            /***fake data***/
            //Line lx5k1 = new Line(x5, k1);
            //lx5k1.parent = lx56;
            //Line lk9x4 = new Line(k9, x4);
            //lk9x4.parent = lx34;
            //Line ly6k2 = new Line(y6, k2);
            //ly6k2.parent = ly61;
            //Line lk10y6 = new Line(k10, y6);
            //lk10y6.parent = ly56;

            //cell11.addLine(lk10y6);
            //cell11.addLine(lx5k1);
            //cell11.addLine(lx45);
            //cell11.addLine(lk9x4);
            //cell11.addLine(ly6k2);
            Cell allCell = new Cell(1);
            allCell.addLine(lx12);
            allCell.addLine(lx23);
            allCell.addLine(lx34);
            allCell.addLine(lx45);
            allCell.addLine(lx56);
            allCell.addLine(lx61);
            allCell.addLine(lx710);
            allCell.addLine(lx109);
            allCell.addLine(lx98);
            allCell.addLine(lx87);

            allCell.addLine(ly12);
            allCell.addLine(ly23);
            allCell.addLine(ly34);
            allCell.addLine(ly45);
            allCell.addLine(ly56);
            allCell.addLine(ly61);
            allCell.addLine(ly710);
            allCell.addLine(ly109);
            allCell.addLine(ly98);
            allCell.addLine(ly87);
            IIntersector intersector = new SweepLineII();
            //cell11.setIntersectPoint(intersector.Intersect(cell11.getLines()));
            //foreach (var p in cell11.getIntersectPoint())
            //{
            //    Console.Write(p + ",");
            //}

            allCell.setIntersectPoint(intersector.Intersect(allCell.getLines()));
            foreach (var p in allCell.getIntersectPoint())
            {
                Console.Write(p + ",");
            }
            int a = 3;

        }
        #region construction
        public static void construction()
         {
             Point x1 = new Point(8, 12);
             Point x2 = new Point(10, 8);
             Point x3 = new Point(9, 4.5);
             Point x4 = new Point(4, 4);
             Point x5 = new Point(0.5, 5);
             Point x6 = new Point(2, 8);

             Line lx12 = new Line(x1, x2);
             Line lx23 = new Line(x2, x3);
             Line lx34 = new Line(x3, x4);
             Line lx45 = new Line(x4, x5);
             Line lx56 = new Line(x5, x6);
             Line lx61 = new Line(x6, x1);

             List<Line> temp = new List<Line>();
             temp.Add(lx12);
             temp.Add(lx23);
             temp.Add(lx34);
             temp.Add(lx45);
             temp.Add(lx56);
             temp.Add(lx61);

             Polygon polyx1 = new Polygon(1, temp);
             lx12.setPolygon(polyx1);
             lx23.setPolygon(polyx1);
             lx34.setPolygon(polyx1);
             lx45.setPolygon(polyx1);
             lx56.setPolygon(polyx1);
             lx61.setPolygon(polyx1);

             Point x7 = new Point(8.1, 10);
             Point x8 = new Point(7, 7);
             Point x9 = new Point(5, 7.1);
             Point x10 = new Point(7.5, 10.5);

             Line lx710 = new Line(x7, x10);
             Line lx109 = new Line(x10, x9);
             Line lx98 = new Line(x9, x8);
             Line lx87 = new Line(x8, x7);

             List<Line> temp1 = new List<Line>();
             temp1.Add(lx710);
             temp1.Add(lx109);
             temp1.Add(lx98);
             temp1.Add(lx87);
             Hole Holex7 = new Hole(1, temp1);
             polyx1.addHole(Holex7);
             lx710.setPolygon(Holex7);
             lx109.setPolygon(Holex7);
             lx98.setPolygon(Holex7);
             lx87.setPolygon(Holex7);

             Point y1 = new Point(3, 8);
             Point y2 = new Point(6, 9);
             Point y3 = new Point(8.2, 8);
             Point y4 = new Point(8, 6);
             Point y5 = new Point(6.5, 2);
             Point y6 = new Point(1, 1);

             Line ly12 = new Line(y1, y2);
             Line ly23 = new Line(y2, y3);
             Line ly34 = new Line(y3, y4);
             Line ly45 = new Line(y4, y5);
             Line ly56 = new Line(y5, y6);
             Line ly61 = new Line(y6, y1);

             List<Line> temp5 = new List<Line>();
             temp5.Add(ly12);
             temp5.Add(ly23);
             temp5.Add(ly34);
             temp5.Add(ly45);
             temp5.Add(ly56);
             temp5.Add(ly61);
             Polygon polyy1 = new Polygon(2, temp5);
             ly12.setPolygon(polyy1);
             ly23.setPolygon(polyy1);
             ly34.setPolygon(polyy1);
             ly45.setPolygon(polyy1);
             ly56.setPolygon(polyy1);
             ly61.setPolygon(polyy1);

             Point y7 = new Point(7, 7.5);
             Point y8 = new Point(7.1, 6.5);
             Point y9 = new Point(6.5, 5.5);
             Point y10 = new Point(6.6, 7.3);

             Line ly710 = new Line(y7, y10);
             Line ly109 = new Line(y10, y9);
             Line ly98 = new Line(y9, y8);
             Line ly87 = new Line(y8, y7);
             List<Line> temp6 = new List<Line>();
             temp6.Add(ly710);
             temp6.Add(ly109);
             temp6.Add(ly98);
             temp6.Add(ly87);
             Hole holey7 = new Hole(2, temp6);
             polyy1.addHole(holey7);
             ly710.setPolygon(holey7);
             ly109.setPolygon(holey7);
             ly98.setPolygon(holey7);
             ly87.setPolygon(holey7);

             Dictionary<int, Cell> cells = new Dictionary<int, Cell>();
             Cell cell00 = new Cell(1);
             cell00.xmin = 0;
             cell00.xmax = 5;
             cell00.ymin = 0;
             cell00.ymax = 6;
             cells.Add(1, cell00);

             Cell cell01 = new Cell(2);
             cell01.xmin = 5;
             cell01.xmax = 10;
             cell01.ymin = 0;
             cell01.ymax = 6;
             cells.Add(2, cell01);

             Cell cell10= new Cell(3);
             cell10.xmin = 0;
             cell10.xmax = 5;
             cell10.ymin = 6;
             cell10.ymax = 12;
             cells.Add(3, cell10);

             Cell cell11 = new Cell(4);
             cell11.xmin = 5;
             cell11.xmax = 10;
             cell11.ymin = 6;
             cell11.ymax = 12;
             cells.Add(4, cell11);

             cell00.setBottomCell(0);
             cell00.setTopCell(3);
             cell00.setLeftCell(0);
             cell00.setRightCell(2);

             cell01.setBottomCell(0);
             cell01.setTopCell(4);
             cell01.setLeftCell(1);
             cell01.setRightCell(0);

             cell10.setBottomCell(1);
             cell10.setTopCell(0);
             cell10.setLeftCell(0);
             cell10.setRightCell(4);

             cell11.setBottomCell(2);
             cell11.setTopCell(0);
             cell11.setLeftCell(3);
             cell11.setRightCell(0);
             GridIntersectPoint k1 = new GridIntersectPoint(1, 6);
             k1.setLine(lx56);
             GridIntersectPoint k2 = new GridIntersectPoint(2.4, 6);
             k2.setLine(ly61);
             GridIntersectPoint k3 = new GridIntersectPoint(5, 10);
             k3.setLine(lx61);
             GridIntersectPoint k4 = new GridIntersectPoint(5, 8.7);
             k4.setLine(ly12);
             GridIntersectPoint k5 = new GridIntersectPoint(6.5, 6);
             k5.setLine(ly109);
             GridIntersectPoint k6 = new GridIntersectPoint(6.8, 6);
             k6.setLine(ly98);
             GridIntersectPoint k7 = new GridIntersectPoint(8, 6);
             k7.setLine(ly34);
             GridIntersectPoint k8 = new GridIntersectPoint(9.4, 6);
             k8.setLine(lx23);
             GridIntersectPoint k9 = new GridIntersectPoint(5, 4.1);
             k9.setLine(lx34);
             GridIntersectPoint k10 = new GridIntersectPoint(5, 1.7);
             k10.setLine(ly56);
             GridIntersectPoint k11 = new GridIntersectPoint(8, 12);
             GridIntersectPoint k12 = new GridIntersectPoint(10, 8);
             GridIntersectPoint k13 = new GridIntersectPoint(5, 7.1);
             
             //configure intersect points
             
             IntersectPoint z6 = new IntersectPoint(2.0, 4.6);
             z6.inComingLine = ly61;
             z6.outGoingLine = lx45;


             IntersectPoint z5 = new IntersectPoint(7.4, 4.3);
             z5.inComingLine = lx34;
             z5.outGoingLine = ly45;

             //configure the lines

             ly61.clearPoint();
             ly61.addPoint(y6);
             ly61.addPoint(z6);
             ly61.addPoint(k2);
             ly61.addPoint(y1);

             ly12.clearPoint();
             ly12.addPoint(y1);
             ly12.addPoint(k4);
             ly12.addPoint(y2);

             ly23.clearPoint();
             ly23.addPoint(y2);
             ly23.addPoint(y3);

             ly34.clearPoint();
             ly34.addPoint(y3);
             ly34.addPoint(k7);

             ly45.clearPoint();
             //ly45.addPoint(y4);
             ly45.addPoint(k7);
             ly45.addPoint(z5);
             ly45.addPoint(y5);
             
             ly56.clearPoint();
             ly56.addPoint(y5);
             ly56.addPoint(k10);
             ly56.addPoint(y6);

             lx12.clearPoint();
             lx12.addPoint(k11);
             lx12.addPoint(k12);

             lx23.clearPoint();
             lx23.addPoint(k12);
             lx23.addPoint(k8);
             lx23.addPoint(x3);

             lx34.clearPoint();
             lx34.addPoint(x3);
             lx34.addPoint(z5);
             lx34.addPoint(k9);
             lx34.addPoint(x4);

             lx45.clearPoint();
             lx45.addPoint(x4);
             lx45.addPoint(z6);
             lx45.addPoint(x5);

             lx56.clearPoint();
             lx56.addPoint(x5);
             lx56.addPoint(k1);
             lx56.addPoint(x6);

             lx61.clearPoint();
             lx61.addPoint(x6);
             lx61.addPoint(k3);
             lx61.addPoint(k11);

             IEnumerable<LinkedPoint> uptemp = null;
             IEnumerable<LinkedPoint> downtemp = null;
             IEnumerable<LinkedPoint> lefttemp = null;
             IEnumerable<LinkedPoint> righttemp = null;

             //configure for cell00
             Line lx5k1 = new Line(x5, k1);
             lx5k1.parent=lx56;
             Line lk9x4 = new Line(k9, x4);
             lk9x4.parent=lx34;
             Line ly6k2 = new Line(y6, k2);
             ly6k2.parent = ly61;
             Line lk10y6 = new Line(k10, y6);
             lk10y6.parent = ly56;
             
             cell00.addLine(lk10y6);
             cell00.addLine(lx5k1);
             cell00.addLine(lx45);
             cell00.addLine(lk9x4);
             cell00.addLine(ly6k2);
            List <GridIntersectPoint> points=new List<GridIntersectPoint>();
            points.Add(k1);
            points.Add(k2);
            cell00.setTopGridIntersectPoint(points);
            points = new List<GridIntersectPoint>();
            points.Add(k9);
            points.Add(k10);
            cell00.setRightGridIntersectPoint(points);
            List<IntersectPoint> iPoints = new List<IntersectPoint>();
            iPoints.Add(z6);
            cell00.setIntersectPoint(iPoints); 
            PolygonReconstuctor reconstuctor = new PolygonReconstuctor();
            cell00.postPolys = reconstuctor.reconstruct(cell00.getLines(), cell00.getIntersectPoint(), cell00.getTopGridIntersectPoint(), cell00.getBottomGridIntersectPoint(), cell00.getLeftGridIntersectPoint(), cell00.getRightGridIntersectPoint(), out uptemp, out downtemp, out lefttemp, out righttemp, cell00.xmin, cell00.xmax, cell00.ymin, cell00.ymax) as List<PostPolygon>;
            cell00._upLinkedPoints = uptemp as List<LinkedPoint>;
            cell00._downLinkedPoints = downtemp as List<LinkedPoint>;
            cell00._leftLinkedPoints = lefttemp as List<LinkedPoint>;
            cell00._rightLinkedPoints = righttemp as List<LinkedPoint>;
            //reconstuctor.reconstruct(cell00.getLines(), cell00.getIntersectPoint(), cell00.getTopGridIntersectPoint(), cell00.getBottomGridIntersectPoint(), cell00.getLeftGridIntersectPoint(), cell00.getRightGridIntersectPoint(), cell00.xmin, cell00.xmax, cell00.ymin, cell00.ymax);

             //configure for cell01
             Line lx3k9=new Line(x3,k9);
             lx3k9.parent=lx34;
             Line lk8x3=new Line(k8,x3);
             lk8x3.parent=lx23;
             Line lk7y5=new Line(k7,y5);
             lk7y5.parent=ly45;
             Line ly5k10 = new Line(y5, k10);
             ly5k10.parent = ly56;

             cell01.addLine(lx3k9);
             cell01.addLine(lk8x3);
             cell01.addLine(lk7y5);
             cell01.addLine(ly5k10);

             points = new List<GridIntersectPoint>();
             points.Add(k9);
             points.Add(k10); 
             cell01.setLeftGridIntersectPoint(points);
             points = new List<GridIntersectPoint>();
             points.Add(k7);
             points.Add(k8);
             cell01.setTopGridIntersectPoint(points);
             iPoints = new List<IntersectPoint>();
             iPoints.Add(z5);
             cell01.setIntersectPoint(iPoints);
             cell01.postPolys = reconstuctor.reconstruct(cell01.getLines(), cell01.getIntersectPoint(), cell01.getTopGridIntersectPoint(), cell01.getBottomGridIntersectPoint(), cell01.getLeftGridIntersectPoint(), cell01.getRightGridIntersectPoint(), out uptemp, out downtemp, out lefttemp, out righttemp, cell01.xmin, cell01.xmax, cell01.ymin, cell01.ymax) as List<PostPolygon>;
             cell01._upLinkedPoints = uptemp as List<LinkedPoint>;
             cell01._downLinkedPoints = downtemp as List<LinkedPoint>;
             cell01._leftLinkedPoints = lefttemp as List<LinkedPoint>;
             cell01._rightLinkedPoints = righttemp as List<LinkedPoint>;
             //reconstuctor.reconstruct(cell01.getLines(), cell01.getIntersectPoint(), cell01.getTopGridIntersectPoint(), cell01.getBottomGridIntersectPoint(), cell01.getLeftGridIntersectPoint(), cell01.getRightGridIntersectPoint(), cell01.xmin, cell01.xmax, cell01.ymin, cell01.ymax);

             //configure for cell10
             Line lk1x6 = new Line(k1, x6);
             lk1x6.parent = lx56;
             Line lx6k3 = new Line(x6, k3);
             lx6k3.parent = lx61;
             Line lk2y1 = new Line(k2, y1);
             lk2y1.parent = ly61;
             Line ly1k4 = new Line(y1, k4);
             ly1k4.parent = ly12;

             cell10.addLine(lk1x6);
             cell10.addLine(lx6k3);
             cell10.addLine(lk2y1);
             cell10.addLine(ly1k4);

             points = new List<GridIntersectPoint>();
             points.Add(k3);
             points.Add(k4);
             cell10.setRightGridIntersectPoint(points);
             points = new List<GridIntersectPoint>();
             points.Add(k1);
             points.Add(k2);
             cell10.setBottomGridIntersectPoint(points);
             iPoints = new List<IntersectPoint>();
             cell10.setIntersectPoint(iPoints);
             cell10.postPolys = reconstuctor.reconstruct(cell10.getLines(), cell10.getIntersectPoint(), cell10.getTopGridIntersectPoint(), cell10.getBottomGridIntersectPoint(), cell10.getLeftGridIntersectPoint(), cell10.getRightGridIntersectPoint(), out uptemp, out downtemp, out lefttemp, out righttemp, cell10.xmin, cell10.xmax, cell10.ymin, cell10.ymax) as List<PostPolygon>;
             cell10._upLinkedPoints = uptemp as List<LinkedPoint>;
             cell10._downLinkedPoints = downtemp as List<LinkedPoint>;
             cell10._leftLinkedPoints = lefttemp as List<LinkedPoint>;
             cell10._rightLinkedPoints = righttemp as List<LinkedPoint>;
             //reconstuctor.reconstruct(cell10.getLines(), cell10.getIntersectPoint(), cell10.getTopGridIntersectPoint(), cell10.getBottomGridIntersectPoint(), cell10.getLeftGridIntersectPoint(), cell10.getRightGridIntersectPoint(), cell10.xmin, cell10.xmax, cell10.ymin, cell10.ymax);

             // configure cell11
             Line lk3x1 = new Line(k3, x1);
             lk3x1.parent = lx61;
             Line lx2k8 = new Line(x2, k8);
             lx2k8.parent = lx23;
             Line lk4y2 = new Line(k4, y2);
             lk4y2.parent = ly12;
             Line ly3k7 = new Line(y3, k7);
             ly3k7.parent = ly34;


             cell11.addLine(lk3x1);
             cell11.addLine(lx12);
             cell11.addLine(lx2k8);
             cell11.addLine(lk4y2);
             cell11.addLine(ly23);
             cell11.addLine(ly3k7);

             //Line lx10k13 = new Line(x10, k13);
             
             //cell11.addLine(lx710);
             //cell11.addLine(lk4y2);
             //cell11.addLine(ly23);
             //cell11.addLine(ly3k7);
             //cell11.addLine(lx2k8);
             //cell11.addLine(lk4y2);
             //cell11.addLine(ly23);
             //cell11.addLine(ly3k7);
            
            
             points = new List<GridIntersectPoint>();
             points.Add(k3);
             points.Add(k4);
             cell11.setLeftGridIntersectPoint(points);
             points = new List<GridIntersectPoint>();
             points.Add(k12);
             cell11.setRightGridIntersectPoint(points);
             points = new List<GridIntersectPoint>();
             points.Add(k11);
             cell11.setTopGridIntersectPoint(points);
             points = new List<GridIntersectPoint>();
             points.Add(k7);
             points.Add(k8);
             cell11.setBottomGridIntersectPoint(points);
             iPoints = new List<IntersectPoint>();
             cell11.setIntersectPoint(iPoints);

             cell11.postPolys=reconstuctor.reconstruct(cell11.getLines(), cell11.getIntersectPoint(), cell11.getTopGridIntersectPoint(), cell11.getBottomGridIntersectPoint(), cell11.getLeftGridIntersectPoint(), cell11.getRightGridIntersectPoint(), out uptemp, out downtemp, out lefttemp, out righttemp, cell11.xmin, cell11.xmax, cell11.ymin, cell11.ymax) as List<PostPolygon>;
             cell11._upLinkedPoints = uptemp as List<LinkedPoint>;
             cell11._downLinkedPoints = downtemp as List<LinkedPoint>;
             cell11._leftLinkedPoints = lefttemp as List<LinkedPoint>;
             cell11._rightLinkedPoints = righttemp as List<LinkedPoint>;

             IMergable merger = new LinkedMerge();
             //merger.merge(cells as Dictionary<int,Cell>);
             IFileHandler handler_overlayer = new GMLFileHandler(Constants.overLayerId);
             handler_overlayer.Write(merger.merge(cells as Dictionary<int,Cell>), "../../../GISUnitTests/Test Data/result.gml");
        }
        #endregion

        // Test case for topology reconstruction. Ask Xi for the polygon diagram 
        // Written by Xi
        #region
        /*
        public static void testTopologyReconstruction()
        {
            Point x1 = new Point(8, 12);
            Point x2 = new Point(10,8);
            Point x3 = new Point(9,4.5);
            Point x4 = new Point(4,4);
            Point x5 = new Point(0.5,5);
            Point x6 = new Point(2,8);
            Point x7 = new Point(8.1,10);
            Point x8 = new Point(7,7);
            Point x9 = new Point(5,7.1);
            Point x10 = new Point(7.5,10.5);
            Point y1 = new Point(3,8);
            Point y2 = new Point(6,9);
            Point y3 = new Point(8.2,8);
            Point y4 = new Point(8,6);
            Point y5 = new Point(6.5,2);
            Point y6 = new Point(1,1);
            Point y7 = new Point(7,7.5);
            Point y8 = new Point(7.1,6.5);
            Point y9 = new Point(6.5,5.5);
            Point y10 = new Point(6.6,7.3);
            Line l1 = new Line(y9, y10);
            Line l2 = new Line(x8, x9);
            IntersectPoint z1 = new IntersectPoint(6.3, 8.9);
            IntersectPoint z2 = new IntersectPoint(7.5, 8.3);
            IntersectPoint z3 = new IntersectPoint(7.0, 7.1);
            IntersectPoint z4= new IntersectPoint(6.6, 7.0);
            IntersectPoint z5 = new IntersectPoint(7.4, 4.3);
            IntersectPoint z6 = new IntersectPoint(2.0, 4.6);

            IntersectPoint result = null;
            Intersection test = l1.getIntersect(l2,out result);
             if (test == Intersection.True)
             { 
              Console.WriteLine("Intersection x = " + result.getX() + " y = " + result.getY());
             }
            
            List<Point> list1=new List<Point>();
            list1.Add(x1);
            list1.Add(x2);
            list1.Add(x3);
            list1.Add(z5);
            z5.setInLine(list1);
            z5.setInPos(3);
            list1.Add(x4);
            list1.Add(z6);
            z6.setOutLine(list1);
            z6.setOutPos(5);
            list1.Add(x5);
            list1.Add(x6);
            //list1.Add(x1);

            List<Point> list2 = new List<Point>();
            list2.Add(y1);
            list2.Add(y2);
            list2.Add(z1);
            z1.setInLine(list2);
            z1.setInPos(2);
            list2.Add(z2);
            z2.setOutLine(list2);
            z2.setOutPos(3);
            list2.Add(y3);
            list2.Add(y4);
            list2.Add(z5);
            z5.setOutLine(list2);
            z5.setOutPos(6);
            list2.Add(y5);
            list2.Add(y6);
            list2.Add(z6);
            z6.setInLine(list2);
            z6.setInPos(9);
            //list1.Add(y1);

            List<Point> list3 = new List<Point>();
            list3.Add(x7);
            list3.Add(x10);
            list3.Add(z1);
            z1.setOutPos(2);
            z1.setOutLine(list3);
            list3.Add(x9);
            list3.Add(z4);
            z4.setOutLine(list3);
            z4.setOutPos(4);
            list3.Add(x8);
            list3.Add(z3);
            z3.setInLine(list3);
            z3.setInPos(6);
            list3.Add(z2);
            z2.setInLine(list3);
            z2.setInPos(7);
            //list3.Add(x7);

            List<Point> list4 = new List<Point>();
            list4.Add(y7);
            list4.Add(y10);
            list4.Add(z4);
            z4.setInLine(list4);
            z4.setInPos(2);
            list4.Add(y9);
            list4.Add(y8);
            list4.Add(z3);
            z3.setOutLine(list4);
            z3.setOutPos(5);
            //list4.Add(y7);

            List<IntersectPoint> list5 = new List<IntersectPoint>();
            //list5.Add(z1);
            //list5.Add(z2);
            //list5.Add(z3);
            //list5.Add(z4);
            list5.Add(z5);
            list5.Add(z6);
            Cell smallCell = new Cell(1);
            //smallCell.intersect(list5);
            List<IntersectPoint> list7 = new List<IntersectPoint>();
            List<IntersectPoint> list8 = new List<IntersectPoint>();
            list7.Add(z5);
            list7.Add(z6);
            list8.Add(z3);
            list8.Add(z4);
            //smallCell.union(list7, list8);
        }
        */
        #endregion
        //Xi's test case for integration with Sweep line algorithm
        #region
        /*
        public static void testIntegration()
        {
            
            Point x1 = new Point(8, 12);
            Point x2 = new Point(10, 8);
            Point x3 = new Point(9, 4.5);
            Point x4 = new Point(4, 4);
            Point x5 = new Point(0.5, 5);
            Point x6 = new Point(2, 8);

            Line lx12 = new Line(x1, x2);
            Line lx23 = new Line(x2, x3);
            Line lx34 = new Line(x3, x4);
            Line lx45 = new Line(x4, x5);
            Line lx56 = new Line(x5, x6);
            Line lx61 = new Line(x6, x1);

            List<Line> temp = new List<Line>();
            temp.Add(lx12);
            temp.Add(lx23);
            temp.Add(lx34);
            temp.Add(lx45);
            temp.Add(lx56);
            temp.Add(lx61);

            Polygon polyx1 = new Polygon(1, temp);
            lx12.setPolygon(polyx1);
            lx23.setPolygon(polyx1);
            lx34.setPolygon(polyx1);
            lx45.setPolygon(polyx1);
            lx56.setPolygon(polyx1);
            lx61.setPolygon(polyx1);

            //Point x7 = new Point(8.1, 10);
            //Point x8 = new Point(7, 7);
            //Point x9 = new Point(5, 7.1);
            //Point x10 = new Point(7.5, 10.5);

            //Line lx710 = new Line(x7, x10);
            //Line lx109 = new Line(x10, x9);
            //Line lx98 = new Line(x9, x8);
            //Line lx87 = new Line(x8, x7);

            //List<Line> temp1 = new List<Line>();
            //temp1.Add(lx710);
            //temp1.Add(lx109);
            //temp1.Add(lx98);
            //temp1.Add(lx87);
            //Hole Holex7 = new Hole(1, temp1);
            //polyx1.addHole(Holex7);
            //lx710.setPolygon(Holex7);
            //lx109.setPolygon(Holex7);
            //lx98.setPolygon(Holex7);
            //lx87.setPolygon(Holex7);
           

            //Point x11 = new Point(3.8, 6);
            //Point x12 = new Point(3.4, 6);
            //Point x13 = new Point(3.4, 5);
            //Point x14 = new Point(3.8, 5);

            //Line lx1112 = new Line(x11, x12);
            //Line lx1213 = new Line(x12, x13);
            //Line lx1314 = new Line(x13, x14);
            //Line lx1411 = new Line(x14, x11);
            //List<Line> temp2 = new List<Line>();
            //temp2.Add(lx1112);
            //temp2.Add(lx1213);
            //temp2.Add(lx1314);
            //temp2.Add(lx1411);
            //Hole Holex11 = new Hole(1, temp2);
            //polyx1.addHole(Holex11);
            
            //lx1112.setPolygon(Holex11);
            //lx1213.setPolygon(Holex11);
            //lx1314.setPolygon(Holex11);
            //lx1411.setPolygon(Holex11);
    

            //Point x15 = new Point(1.8, 6);
            //Point x16 = new Point(1.5, 6);
            //Point x17 = new Point(1.5, 5);
            //Point x18 = new Point(1.8, 5);

            //Line lx1516 = new Line(x15, x16);
            //Line lx1617 = new Line(x16, x17);
            //Line lx1718 = new Line(x17, x18);
            //Line lx1815 = new Line(x18, x15);
            //List<Line> temp3 = new List<Line>();
            //temp3.Add(lx1516);
            //temp3.Add(lx1617);
            //temp3.Add(lx1718);
            //temp3.Add(lx1815);
            //Hole Holex15 = new Hole(1, temp3);
            //polyx1.addHole(Holex15);
            //lx1516.setPolygon(Holex15);
            //lx1617.setPolygon(Holex15);
            //lx1718.setPolygon(Holex15);
            //lx1815.setPolygon(Holex15);


            //Point x19 = new Point(7, 15);
            //Point x20 = new Point(2, 15);
            //Point x21 = new Point(2, 12);
            //Point x22 = new Point(7, 12);

            //Line lx1920 = new Line(x19, x20);
            //Line lx2021 = new Line(x20, x21);
            //Line lx2122 = new Line(x21, x22);
            //Line lx2219 = new Line(x22, x19);
            //List<Line> temp4 = new List<Line>();
            //temp4.Add(lx1920);
            //temp4.Add(lx2021);
            //temp4.Add(lx2122);
            //temp4.Add(lx2219);
            //Polygon Poly19 = new Polygon(1, temp4);
            //lx1920.setPolygon(Poly19);
            //lx2021.setPolygon(Poly19);
            //lx2122.setPolygon(Poly19);
            //lx2219.setPolygon(Poly19);
    

            Point y1 = new Point(3, 8);
            Point y2 = new Point(6, 9);
            Point y3 = new Point(8.2, 8);
            Point y4 = new Point(8, 6);
            Point y5 = new Point(6.5, 2);
            Point y6 = new Point(1, 1);

            Line ly12 = new Line(y1, y2);
            Line ly23 = new Line(y2, y3);
            Line ly34 = new Line(y3, y4);
            Line ly45 = new Line(y4, y5);
            Line ly56 = new Line(y5, y6);
            Line ly61 = new Line(y6, y1);

            List<Line> temp5 = new List<Line>();
            temp5.Add(ly12);
            temp5.Add(ly23);
            temp5.Add(ly34);
            temp5.Add(ly45);
            temp5.Add(ly56);
            temp5.Add(ly61);
            Polygon polyy1 = new Polygon(2, temp5);
            ly12.setPolygon(polyy1);
            ly23.setPolygon(polyy1);
            ly34.setPolygon(polyy1);
            ly45.setPolygon(polyy1);
            ly56.setPolygon(polyy1);
            ly61.setPolygon(polyy1);

            //Point y7 = new Point(7, 7.5);
            //Point y8 = new Point(7.1, 6.5);
            //Point y9 = new Point(6.5, 5.5);
            //Point y10 = new Point(6.6, 7.3);

            //Line ly710 = new Line(y7, y10);
            //Line ly109 = new Line(y10, y9);
            //Line ly98 = new Line(y9, y8);
            //Line ly87 = new Line(y8, y7);
            //List<Line> temp6 = new List<Line>();
            //temp6.Add(ly710);
            //temp6.Add(ly109);
            //temp6.Add(ly98);
            //temp6.Add(ly87);
            //Hole holey7 = new Hole(2, temp6);
            //polyy1.addHole(holey7);
            //ly710.setPolygon(holey7);
            //ly109.setPolygon(holey7);
            //ly98.setPolygon(holey7);
            //ly87.setPolygon(holey7);

            //Point y11 = new Point(18, 10);
            //Point y12= new Point(18, 5);
            //Point y13= new Point(14, 5);
            //Point y14 = new Point(14, 10);
            
            //Line ly1112 = new Line(y11, y12);
            //Line ly1213 = new Line(y12, y13);
            //Line ly1314 = new Line(y13, y14);
            //Line ly1411 = new Line(y14, y11);
            //List<Line> temp7 = new List<Line>();
            //temp7.Add(ly1112);
            //temp7.Add(ly1213);
            //temp7.Add(ly1314);
            //temp7.Add(ly1411);
            //Hole polyy7 = new Hole(2, temp7);
            //ly1112.setPolygon(polyy7);
            //ly1213.setPolygon(polyy7);
            //ly1314.setPolygon(polyy7);
            //ly1411.setPolygon(polyy7);

            Cell testCell = new Cell(1);
            //testCell.addLine(lx12);
            //testCell.addLine(lx23);
            //testCell.addLine(ly710);
            
            testCell.addLine(lx34);
            testCell.addLine(lx45);
            testCell.addLine(lx56);
            testCell.addLine(lx61);
            //testCell.addLine(lx710);
            //testCell.addLine(lx109);
            //testCell.addLine(lx98);
            //testCell.addLine(lx87);
            //testCell.addLine(lx1112);
            //testCell.addLine(lx1213);
            //testCell.addLine(lx1314);
            //testCell.addLine(lx1411);
            //testCell.addLine(lx1516);
            //testCell.addLine(lx1617);
            //testCell.addLine(lx1718);
            //testCell.addLine(lx1815);
            //testCell.addLine(lx1920);
            //testCell.addLine(lx2021);
            //testCell.addLine(lx2122);
            //testCell.addLine(lx2219);
            testCell.addLine(ly12);
            testCell.addLine(ly23);
            testCell.addLine(ly34);
            testCell.addLine(ly45);
            testCell.addLine(ly56);
            testCell.addLine(ly61);
            
            //testCell.addLine(ly109);
            //testCell.addLine(ly98);
            //testCell.addLine(ly87);
            //testCell.addLine(ly1112);
            //testCell.addLine(ly1213);
            //testCell.addLine(ly1314);
            //testCell.addLine(ly1411);

            GridIntersectPoint gx1x2 = new GridIntersectPoint(9.5, 9.0);
            GridIntersectPoint gx2x3 = new GridIntersectPoint(9.5, 6.25);
            Line line1 = new Line(x1, gx1x2);
            Line line2 = new Line(gx1x2, x2);
            Line line3 = new Line(x2, gx2x3);
            Line line4=new Line(gx2x3, x3);
            line1.parent = lx12;
            line2.parent = lx12;
            line3.parent = lx23;
            line4.parent = lx23;
            testCell.addLine(line1);
            testCell.addLine(line2);
            testCell.addLine(line3);
            testCell.addLine(line4);
            
            lx12.clearPoint();
            lx12.addPoint(x1);
            lx12.addPoint(gx1x2);
            lx12.addPoint(x2);

            lx23.clearPoint();
            lx23.addPoint(x2);
            lx23.addPoint(gx2x3);
            lx23.addPoint(x3);

            //ly710.clearPoint();
            //ly710.addPoint(y7);
            //ly710.addPoint(y10);

            //IntersectPoint z1 = new IntersectPoint(6.3, 8.9);
            //z1.setLine1(lx109);
            //z1.setLine2(ly23);
            //IntersectPoint z2 = new IntersectPoint(7.5, 8.3);
            //z2.setLine1(lx87);
            //z2.setLine2(ly23);
            //IntersectPoint z3 = new IntersectPoint(7.0, 7.1);
            //z3.setLine1(ly87);
            //z3.setLine2(lx87);
            //IntersectPoint z4 = new IntersectPoint(6.6, 7.0);
            //z4.setLine1(ly109);
            //z4.setLine2(lx98);
            IntersectPoint z5 = new IntersectPoint(7.4, 4.3);
            z5.setLine1(lx34);
            z5.setLine2(ly45);
            IntersectPoint z6 = new IntersectPoint(2.0, 4.6);
            z6.setLine1(ly61);
            z6.setLine2(lx45);

            ////z1
            //lx109.clearPoint();
            //lx109.addPoint(x10);
            //lx109.addPoint(z1);
            //lx109.addPoint(x9);
            ////z2
            //ly23.clearPoint();
            //ly23.addPoint(y2);
            //ly23.addPoint(z1);
            //ly23.addPoint(z2);
            //ly23.addPoint(y3);
            ////z3
            //lx87.clearPoint();
            //lx87.addPoint(x8);
            //lx87.addPoint(z3);
            //lx87.addPoint(z2);
            //lx87.addPoint(x7);

            //ly87.clearPoint();
            //ly87.addPoint(y8);
            //ly87.addPoint(z3);
            //ly87.addPoint(y7);
            ////z4
            //lx98.clearPoint();
            //lx98.addPoint(x9);
            //lx98.addPoint(z4);
            //lx98.addPoint(x8);

            //ly109.clearPoint();
            //ly109.addPoint(y10);
            //ly109.addPoint(z4);
            //ly109.addPoint(y9);

            //z5
            lx34.clearPoint();
            lx34.addPoint(x3);
            lx34.addPoint(z5);
            lx34.addPoint(x4);

            ly45.clearPoint();
            ly45.addPoint(y4);
            ly45.addPoint(z5);
            ly45.addPoint(y5);
            //z6
            lx45.clearPoint();
            lx45.addPoint(x4);
            lx45.addPoint(z6);
            lx45.addPoint(x5);

            ly61.clearPoint();
            ly61.addPoint(y6);
            ly61.addPoint(z6);
            ly61.addPoint(y1);


            testCell.xmin = 0;
            testCell.xmax = 9.5;
            testCell.ymin = 0;
            testCell.ymax = 13;
            List <IntersectPoint> points=new List<IntersectPoint>();
            //points.Add(z1);
            //points.Add(z2);
            //points.Add(z3);
            //points.Add(z4);
            points.Add(z5);
            points.Add(z6);
            testCell.setPoints(points);
            testCell.setup();
            testCell.intersect();
        }
        */
        #endregion
        #region finding intersect point
        //test case for finding intersect point
        /*
        public static void testFindingIntersectPoint()
        {
            Point x1 = new Point(2, 8);
            Point x2 = new Point(8, 12);
            Point y1 = new Point(2, 4.6);
            Point y2 = new Point(1, 1);
            Line l1 = new Line(x1, x2);
            Line l2 = new Line(y1, y2);
            IntersectPoint result = null;
            Intersection test = l1.getIntersect(l2, out result);
            if (test == Intersection.True)
            {
                Console.WriteLine("Intersection x = " + result.getX() + " y = " + result.getY());
            }
        }
        */
        #endregion
        #region merging
        /*
        //test case for merging
        public static void testMerging()
        {
            Cell cell1 = new Cell(1);
            cell1.xmin = 1;
            cell1.xmax = 4;
            cell1.ymin = 1;
            cell1.ymax = 4;

            Cell cell2 = new Cell(2);
            cell1.xmin = 4;
            cell1.xmax = 8;
            cell1.ymin = 1;
            cell1.ymax = 4;

            Cell cell3 = new Cell(3);
            cell1.xmin = 1;
            cell1.xmax = 4;
            cell1.ymin = 4;
            cell1.ymax = 8;

            Cell cell4 = new Cell(4);
            cell1.xmin = 4;
            cell1.xmax = 8;
            cell1.ymin = 4;
            cell1.ymax = 8;

            Point pa=new Point(2,4);
            LinkedPoint lpa1 = new LinkedPoint(pa);
            LinkedPoint lpa3 = new LinkedPoint(pa);
            Point pb = new Point(4, 2);
            LinkedPoint lpb1 = new LinkedPoint(pb);
            LinkedPoint lpb2 = new LinkedPoint(pb);
            Point pc = new Point(4, 3);
            LinkedPoint lpc1 = new LinkedPoint(pc);
            LinkedPoint lpc2 = new LinkedPoint(pc);
            Point pd = new Point(3, 4);
            LinkedPoint lpd1 = new LinkedPoint(pd);
            LinkedPoint lpd3 = new LinkedPoint(pd);

            Point pe = new Point(6, 4);
            LinkedPoint lpe2 = new LinkedPoint(pe);
            LinkedPoint lpe4 = new LinkedPoint(pe);
            Point pf = new Point(7, 4);
            LinkedPoint lpf2 = new LinkedPoint(pf);
            LinkedPoint lpf4 = new LinkedPoint(pf);
            Point pg = new Point(4, 6);
            LinkedPoint lpg3 = new LinkedPoint(pg);
            LinkedPoint lpg4 = new LinkedPoint(pg);
            Point ph = new Point(4, 7);
            LinkedPoint lph3 = new LinkedPoint(ph);
            LinkedPoint lph4 = new LinkedPoint(ph);

            lpa1.setNext(lpb1);
            lpb1.setNext(lpc1);
            lpc1.setNext(lpd1);
            lpd1.setNext(lpa1);
            lpa1.setPre(lpd1);
            lpb1.setPre(lpa1);
            lpc1.setPre(lpb1);
            lpd1.setPre(lpc1);
            PostPolygon postPoly1=new PostPolygon(lpa1);
            lpa1.setPostPolygon(postPoly1);
            lpb1.setPostPolygon(postPoly1);
            lpc1.setPostPolygon(postPoly1);
            lpd1.setPostPolygon(postPoly1);

            lpb2.setNext(lpf2);
            lpf2.setNext(lpe2);
            lpe2.setNext(lpc2);
            lpc2.setNext(lpb2);
            lpb2.setPre(lpc2);
            lpc2.setPre(lpe2);
            lpe2.setPre(lpf2);
            lpf2.setPre(lpb2);
            PostPolygon postPoly2=new PostPolygon(lpb2);
            lpb2.setPostPolygon(postPoly2);
            lpf2.setPostPolygon(postPoly2);
            lpe2.setPostPolygon(postPoly2);
            lpc2.setPostPolygon(postPoly2);

            lpa3.setNext(lpd3);
            lpd3.setNext(lpg3);
            lpg3.setNext(lph3);
            lph3.setNext(lpa3);
            lpa3.setPre(lph3);
            lph3.setPre(lpg3);
            lpg3.setPre(lpd3);
            lpd3.setPre(lpa3);
            PostPolygon postPoly3=new PostPolygon(lpa3);
            lpa3.setPostPolygon(postPoly3);
            lpd3.setPostPolygon(postPoly3);
            lpg3.setPostPolygon(postPoly3);
            lph3.setPostPolygon(postPoly3);

            lpe4.setNext(lpf4);
            lpf4.setNext(lph4);
            lph4.setNext(lpg4);
            lpg4.setNext(lpe4);
            lpe4.setPre(lpg4);
            lpg4.setPre(lph4);
            lph4.setPre(lpf4);
            lpf4.setPre(lpe4);
            PostPolygon postPoly4=new PostPolygon(lpe4);
            lpe4.setPostPolygon(postPoly4);
            lpf4.setPostPolygon(postPoly4);
            lph4.setPostPolygon(postPoly4);
            lpg4.setPostPolygon(postPoly4);

            cell1.addPostPolygons(postPoly1);
            cell2.addPostPolygons(postPoly2);
            cell3.addPostPolygons(postPoly3);
            cell4.addPostPolygons(postPoly4);

            //cell1.rightPoints.Add(lpb1);
            //cell1.rightPoints.Add(lpc1);
            //cell1.downPoints.Add(lpa1);
            //cell1.downPoints.Add(lpd1);

            //cell2.leftPoints.Add(lpb2);
            //cell2.leftPoints.Add(lpc2);
            //cell2.downPoints.Add(lpe2);
            //cell2.downPoints.Add(lpf2);

            //cell3.rightPoints.Add(lpg3);
            //cell3.rightPoints.Add(lph3);
            //cell3.upPoints.Add(lpa3);
            //cell3.upPoints.Add(lpd3);

            //cell4.rightPoints.Add(lpg4);
            //cell4.rightPoints.Add(lpg4);
            //cell4.upPoints.Add(lpe4);
            //cell4.upPoints.Add(lpf4);

            Cell[,] cells = new Cell[2, 2];
            cells[0,0] = cell1;
            cells[0,1] = cell2;
            cells[1,0] = cell3;
            cells[1,1] = cell4;

            //UniformGrid.merge(cells,2,2);

        }
        */
        #endregion
        public static void testRealPartition()
        { 
      
        }

        // Satish's test case for partition
        public static void testPartition()
        {
            Point pa = new Point(2, 4);
            Point pd = new Point(4, 6);
            Point pc = new Point(6, 4);
            Point pb = new Point(4, 2);
            Point p4 = new Point(4, 3);
            Point p3 = new Point(7, 6);
            Point p2 = new Point(9, 4);
            Point p1 = new Point(7, 2);



            Line lad = new Line(pa, pd);
            Line ldc = new Line(pd, pc);
            Line lcb = new Line(pc, pb);
            Line lba = new Line(pb, pa);
            Line l43 = new Line(p4, p3);
            Line l32 = new Line(p3, p2);
            Line l21 = new Line(p2, p1);
            Line l14 = new Line(p1, p4);

            lad.next=ldc;
            lad.prev=lba;
            ldc.next=lcb;
            ldc.prev=lad;
            lcb.next=lba;
            lcb.prev=ldc;
            lba.next=lad;
            lba.prev=lcb;

            l43.next=l32;
            l43.prev=l14;
            l32.next=l21;
            l32.prev=l43;
            l21.next=l14;
            l21.prev=l32;
            l14.next=l43;
            l14.prev=l21;


            List<Line> l1 = new List<Line>();
            l1.Add(lad);
            l1.Add(ldc);
            l1.Add(lcb);
            l1.Add(lba);
            Polygon poly1 = new Polygon(1, l1);
            lad.polygon=poly1;
            ldc.polygon=poly1;
            lcb.polygon=poly1;
            lba.polygon=poly1;

            List<Line> l2 = new List<Line>();
            l2.Add(l43);
            l2.Add(l32);
            l2.Add(l21);
            l2.Add(l14);
            Polygon poly2 = new Polygon(2, l2);
            l43.polygon=poly2;
            l32.polygon=poly2;
            l21.polygon=poly2;
            l14.polygon=poly2;
            
            List<Polygon> polyLayer1 = new List<Polygon>();
            polyLayer1.Add(poly1);
            Layer layer1 = new Layer(0, polyLayer1);

            List<Polygon> polyLayer2 = new List<Polygon>();
            polyLayer2.Add(poly2);
            Layer layer2 = new Layer(1, polyLayer2);

            UniformGrid plane = new UniformGrid(10, 10);
            var cells = plane.Partition(layer1, layer2);
        }

        public static void SweepLineIITester()
        {
            new SweepLineIITester().tester();
        }

    }
}
